package com.foreknow.sevlet;

import com.foreknow.model.User;
import com.foreknow.service.impl.UserServiceImpl;
import com.foreknow.service.userService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
        PrintWriter out = resp.getWriter();
        String username=req.getParameter("username");
        String password=req.getParameter("password");
       userService service = new UserServiceImpl();
        User user = service.Login(username,password);
        Gson gson = new Gson();
        if(user!=null){
            String json = gson.toJson(user);
            out.println(json);
        }
        else {
            out.println("登陆失败");
        }

    }
}
